<template>
  <a-layout-header class="header" :style="{padding:'0px 0px'}">
    <div class="logo">Welcome !</div>
    <a-menu
        theme="light"
        mode="horizontal"
        :style="{ lineHeight: '64px' ,background:'#9999cc'}"
    >

      <router-link to="/">
        <a-menu-item key="/">
          <a-space>
            <home-two-tone/>
            <a>首页</a>
          </a-space>
        </a-menu-item>
      </router-link>

      <router-link to="/navigation">
          <a-menu-item key="/navigation">
            <a-space>
              <bulb-two-tone />
              <a>学习导航</a>
            </a-space>
          </a-menu-item>
      </router-link>

      <router-link to="/admin/user">
        <a-menu-item key="/admin/user" :style="user.id? {} : {display:'none'}">
          <a-space>
            <smile-two-tone />
            <a>用户</a>
          </a-space>
        </a-menu-item>
      </router-link>

      <router-link to="/admin/ebook">

          <a-menu-item key="/admin/ebook" :style="user.id? {} : {display:'none'}">
            <a-space>
              <book-two-tone />
              <a>知识之海</a>
            </a-space>
          </a-menu-item>
      </router-link>

      <router-link to="/admin/category">
        <a-menu-item key="/admin/category" :style="user.id? {} : {display:'none'}">
          <a-space>
            <appstore-two-tone />
            <a>知识树</a>
          </a-space>
        </a-menu-item>
      </router-link>

      <a class="login-menu" v-show="!!user.id">
        您好，{{user.name}}
      </a>
      <a-popconfirm
          title="确认退出登录?"
          ok-text="是"
          cancel-text="否"
          @confirm="logout()"
      >
        <a class="login-menu" v-show="!!user.id">
          <span>退出登录</span>
        </a>
      </a-popconfirm>
      <a class="login-menu" v-show="!user.id"  @click="showLoginModal">
        <span>登录</span>
      </a>
    </a-menu>

    <a-modal
        title="登录"
        v-model:visible="loginModalVisible"
        :confirm-loading="loginModalLoading"
        @ok="login"
    >
      <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="登录名">
          <a-input v-model:value="loginUser.loginName" />
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model:value="loginUser.password" type="password" />
        </a-form-item>
      </a-form>
    </a-modal>
  </a-layout-header>
</template>
<script lang="ts">
import { defineComponent, ref, computed } from 'vue';
import {HomeTwoTone,BulbTwoTone,SmileTwoTone,BookTwoTone,AppstoreTwoTone} from '@ant-design/icons-vue';
import axios from 'axios';
import { message } from 'ant-design-vue';
import store from "@/store";
import TheWelcome from "@/components/Welcome.vue";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'the-header',
  components: {
    HomeTwoTone,
    BulbTwoTone,
    SmileTwoTone,
    BookTwoTone,
    AppstoreTwoTone
  },
  setup () {
    // 登录后保存，computed用来监听store
    const user = computed(() => store.state.user);
    // const user = ref();
    // user.value = {};
    // 下面是用来登录测试用的,初始用户名为test,密码为test123
    const loginUser = ref({
      loginName: "test",
      password: "test123"
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    };

    // 登录
    const login = () => {
      console.log("开始登录");
      loginModalLoading.value = true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      axios.post('/user/login', loginUser.value).then((response) => {
        loginModalLoading.value = false;
        const data = response.data;
        if (data.success) {
          loginModalVisible.value = false;
          message.success("登录成功！");
          // user.value = data.content;
          // console.log("用户user的昵称："+user.value.name);
         console.log("data : " , data.content)
          store.commit("setUser", data.content);
        } else {
          message.error(data.message);
        }
      });
    };

    // 退出登录
    const logout = () => {
      console.log("退出登录开始");
      axios.get('/user/logout/' + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("退出登录成功！");
          // 退出登录时，前端要清除store保存的session信息
          store.commit("setUser", {});
        } else {
          message.error(data.message);
        }
      });
    };

    return {
      loginModalVisible,
      loginModalLoading,
      showLoginModal,
      loginUser,
      login,
      user,
      logout
    }
  }
});

</script>

<style>
.logo {
  width: 150px;
  height: 64px;
  /*background: rgba(255, 255, 255, 0.2);*/
  /*margin: 16px 28px 16px 0;*/
  float: left;
  color: white;
  font-size: 18px;
  background-color: #9999cc;
  text-align: center;
}
.login-menu {
  float: right;
  color: black;
  padding-left: 20px;

}

</style>